package com.damei.hospital_android.activity.tabbar;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.damei.hospital_android.R;
import com.damei.hospital_android.activity.StoreActivity;
import com.damei.hospital_android.httpService.HttpManager;
import com.damei.hospital_android.httpService.HttpStringResponseHandler;
import com.damei.hospital_android.model.ItemModel;
import com.damei.hospital_android.utils.Utils;
import com.damei.hospital_android.view.GridAdapter;
import com.xuexiang.xui.widget.layout.XUILinearLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class HomeFragment extends Fragment implements AdapterView.OnItemClickListener {

    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @BindView(R.id.text_home)
    TextView textHome;
    @BindView(R.id.grid_view)
    GridView gridView;
    @BindView(R.id.xui_layout)
    XUILinearLayout layout;

    private Unbinder unbinder;
    private List<ItemModel> menuList;
    private int wmWidth;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        unbinder = ButterKnife.bind(this, view);

        gridView.setOnItemClickListener(this);

        layout.setShadowColor(R.color.teal_200);
        layout.setRadiusAndShadow(20, 20, Float.valueOf("0.7"));

        wmWidth = this.getActivity().getWindowManager().getDefaultDisplay().getWidth();

        gridView.setColumnWidth((wmWidth-40)/4-16);

        getHomeInfoRequest();
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.w("-----------", String.valueOf(position));

        startActivity(new Intent(getContext(), StoreActivity.class));
    }

    private void getHomeInfoRequest() {
        HttpManager.getInstance(getContext()).getHomeInfoRequest("10",
                new HttpStringResponseHandler<List>(getContext(), List.class, true, true) {
                    @Override
                    public void onSuccess(List list) {
                        super.onSuccess(list);

                        Map map = (Map) list.get(1);
                        menuList = new ArrayList<ItemModel>();
                        for (Map obj:(List<Map>)map.get("menuList")) {
                            ItemModel itemModel = Utils.mapToObj(obj, ItemModel.class);
                            menuList.add(itemModel);
                        }

                        layout.setVisibility(View.VISIBLE);
                        GridAdapter adapter = new GridAdapter(getLayoutInflater(), menuList, wmWidth);
                        gridView.setAdapter(adapter);

                        String name = (String) map.get("moduleName");
                        textHome.setText(name);
                    }
                });
    }
}